home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17211 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: news.platinum.com!news
  2. From: Michael Scott <mscott@platinum.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: C++ beginner quesion on data member access.
  5. Date: Fri, 12 Apr 1996 08:05:36 -0700
  6. Organization: Platinum
  7. Message-ID: <316E7140.C5D@platinum.com>
  8. References: <4kgb76$r3s@HOPPER.ACM.ORG>
  9. NNTP-Posting-Host: ns2-ssn.platinum.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. Ken Varn wrote:
  16. >      I have been struggling with the proper way to declare and access data
  17. > members in my classes.  I was once told that a class should not allow any
  18. > data members to be public and that they should only be accessed via member
  19. > functions.  Why?  I know the reasons for declaring private data, but what do
  20. > I gain if all I am doing is providing a member function to get the private
  21. > data or set the private data.  i.e. why call a getData() function that
  22. > basically just returns the private data member as opposed to just declaring
  23. > the pviate data member as public.
  24.  
  25. Classes encapsulate the behavior of an object.  Data members 
  26. represent the "state" of an object.  Data access functions 
  27. provide access to the internal state of an object while allowing 
  28. the implementation of that state to vary without necessarily 
  29. changing the interface to the object.  
  30.  
  31. Pragmatically speaking though, there are classes and 
  32. applications where the trade-offs between encapsulation overhead
  33. and direct public member access favor the public data.
  34.  
  35. Balancing the trade-offs is what makes programming an art rather 
  36. than a strict science.
  37.